package com.hy.controller;

import com.hy.Result.ResponseResult;
import com.hy.medicine.MedicineOrder;
import com.hy.medicine.OrderItem;
import com.hy.medicine.dingdan.dto.OrderCreateDTO;
import com.hy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单管理控制器
 */
@RestController
@RequestMapping("/api/orderMedicine")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param orderCreateDTO 订单创建DTO
     * @return 订单ID
     */
    @PostMapping("/create")
    public ResponseResult createOrder(@RequestBody OrderCreateDTO orderCreateDTO) {
        // 创建订单
        String orderId = orderService.createOrder(orderCreateDTO);

        if (orderId == null) {
            return ResponseResult.errorResult(500, "创建订单失败，可能是库存不足");
        }

        Map<String, Object> result = new HashMap<>();
        result.put("orderId", orderId);
        result.put("totalAmount", orderCreateDTO.getActualPayment());

        return ResponseResult.okResult(result);
    }

    /**
     * 获取订单详情
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("/{orderId}")
    public ResponseResult getOrderDetail(@PathVariable("orderId") String orderId) {
        MedicineOrder order = orderService.getOrderById(orderId);
        if (order == null) {
            return ResponseResult.errorResult(404, "订单不存在");
        }
        return ResponseResult.okResult(order);
    }

    /**
     * 获取用户的订单列表
     * @param userId 用户ID
     * @param status 订单状态(可选)
     * @return 订单列表
     */
    @GetMapping("/list")
    public ResponseResult getOrderList(
            @RequestParam("userId") Long userId,
            @RequestParam(value = "status", required = false) Integer status) {
        List<MedicineOrder> orders = orderService.getOrdersByUserId(userId, status);
        return ResponseResult.okResult(orders);
    }

    /**
     * 获取订单的订单项
     * @param orderId 订单ID
     * @return 订单项列表
     */
    @GetMapping("/{orderId}/items")
    public ResponseResult getOrderItems(@PathVariable("orderId") String orderId) {
        List<OrderItem> items = orderService.getOrderItems(orderId);
        return ResponseResult.okResult(items);
    }

    /**
     * 取消订单
     * @param orderId 订单ID
     * @param userId 用户ID
     * @return 操作结果
     */
    @PostMapping("/cancel")
    public ResponseResult cancelOrder(
            @RequestParam("orderId") String orderId,
            @RequestParam("userId") Long userId) {
        boolean result = orderService.cancelOrder(orderId, userId);
        if (result) {
            return ResponseResult.okResult(true);
        } else {
            return ResponseResult.errorResult(500, "取消订单失败");
        }
    }

    /**
     * 支付订单
     * @param orderId 订单ID
     * @param paymentType 支付方式
     * @return 操作结果
     */
    @PostMapping("/pay")
    public ResponseResult payOrder(
            @RequestParam("orderId") String orderId,
            @RequestParam("paymentType") String paymentType) {
        boolean result = orderService.payOrder(orderId, paymentType);
        if (result) {
            return ResponseResult.okResult(true);
        } else {
            return ResponseResult.errorResult(500, "支付订单失败");
        }
    }

    /**
     * 更新订单状态
     * @param orderId 订单ID
     * @param status 新状态
     * @return 操作结果
     */
    @PutMapping("/status")
    public ResponseResult updateOrderStatus(
            @RequestParam("orderId") String orderId,
            @RequestParam("status") Integer status) {
        boolean result = orderService.updateOrderStatus(orderId, status);
        if (result) {
            return ResponseResult.okResult(true);
        } else {
            return ResponseResult.errorResult(500, "更新订单状态失败");
        }
    }
}
